<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');

class ArticleflowModelArticleflow extends JModel {

    var $_data = null;
    var $_data_temp = null;
    var $_total = null;
    var $_pagination = null;
    var $_user = null;

    // var $role=null;

    function __construct($config = array()) {
        parent::__construct($config);
        global $mainframe, $option;

        // Get the pagination request variables
        $limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
        $limitstart = $mainframe->getUserStateFromRequest($option . '.limitstart', 'limitstart', 0, 'int');

        // In case limit has been changed, adjust limitstart accordingly
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);

        $this->setState('limit', $limit);
        $this->setState('limitstart', $limitstart);
    }

    function getData() {
        //   echo $userrole;
        $user = & JFactory::getUser();
        $role = $user->getParam('role');
        $uid = $user->get('id');
        if (empty($this->_data)) {
            $query = $this->_buildQuery($role, $uid);
            $this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
        }
        return $this->_data;
    }

//    function getData_Temp_Edit()
//    {
//         $user = & JFactory::getUser();
//        $role = $user->getParam('role');
//        $uid = $user->get('id');
//        if (empty($this->_data_temp)) {
//            $query = $this->_buildQuery_temp();
//            $this->_data_temp = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
//        }
//        return $this->_data_temp;
//    }

    function _buildQuery($role, $uid) {

        $where = $this->_buildContentWhere($role, $uid);
        $query = ' SELECT a.*, cc.title as catename, s.title as secname'
        . ' FROM #__content_temp AS a '
        . ' LEFT JOIN #__categories AS cc ON cc.id = a.catid'
        . ' LEFT JOIN #__sections AS s ON s.id = a.sectionid'
        . $where
                . ' ORDER BY a.id DESC'
        ;
        //echo $query;
        return $query;
    }

//    function _buildQuery_temp()
//    {
//        $where = $this->_buildContentWhere_temp();
//        $query = ' SELECT a.*'
//                . ' FROM #__content_temp_edit AS a '
//                . $where
//                . ' ORDER BY a.id DESC'
//        ;
//        //echo $query;
//        return $query;
//    }

    function _buildContentWhere($role, $uid) {
        global $mainframe, $option;
        $db = & JFactory::getDBO();
        // echo $role;

        $filter_state = $mainframe->getUserStateFromRequest($option . 'filter_state', 'filter_state', '', 'word');
        $filter_catid = $mainframe->getUserStateFromRequest($option . 'filter_catid', 'filter_catid', 0, 'int');
        $search = $mainframe->getUserStateFromRequest($option . 'search', 'search', '', 'string');
        $search = JString::strtolower($search);

        $where = array();

        if ($search) {
            $where[] = 'LOWER(a.title) LIKE ' . $db->Quote('%' . $db->getEscaped($search, true) . '%', false);
        }

        if ($role == 1) {

            $where[] = 'a.state = 1 AND a.assigned_user =' . $uid;
        }

        if ($role == 2) {

            $where[] = 'a.state = 2 AND a.assigned_user =' . $uid;
        }
        if ($role == 0) {
            //$where[] = 'a.state = 0//';
        }
        if ($filter_state) {
            if ($filter_state == 'P') {
                $where[] = 'a.state = 1';
            } else if ($filter_state == 'U') {
                $where[] = 'a.state = 0';
            } else if ($filter_state == 'C') {
                $where[] = 'a.state = 2';
            }
        }

        if ($filter_catid > 0) {
            $where[] = 'a.catid = ' . (int) $filter_catid;
        }

        $where = ( count($where) ? ' WHERE ' . implode(' AND ', $where) : '' );

        return $where;
    }

    function getPagination() {
        // Lets load the connamet if it doesn't already exist
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit'));
        }

        return $this->_pagination;
    }

    function getTotal() {
        // Lets load the connamet if it doesn't already exist
        if (empty($this->_total)) {
            $user = & JFactory::getUser();
            $role = $user->getParam('role');
            $uid = $user->get('id');
            $query = $this->_buildQuery($role, $uid);
            $this->_total = $this->_getListCount($query);
        }

        return $this->_total;
    }

    function getUserNameByCreated($id) {
        $db = JFactory::getDBO();
        $query = 'Select u.name  from #__users as u where u.id = ' . $id;
        $db->setQuery($query);

        $this->_user = $db->loadObject();
        return $this->_user;
    }

    function getUserNameByModified($id) {
        $db = JFactory::getDBO();
        $query = 'Select u.name  from #__users as u where u.id = ' . $id;
        $db->setQuery($query);
        $this->_user = $db->loadObject();
        return $this->_user;
    }

}

?>
